package morenoapp.components.common.table.renderer;

import java.awt.Color;
import javax.swing.SwingUtilities;

/**
 * Значение ячейки таблицы со стилем и метаинформацией.
 * @author dmitry.mamonov
 */
public class StyledTableValue {
    private static final StyledTableValue EMPTY = new StyledTableValue("", Color.black, Aligment.Right, null, null);

    protected String value;

    protected Color foreground;

    protected Aligment aligh;

    protected String tooltip;

    protected String description;

    public StyledTableValue(String value, Color foreground, Aligment aligh, String tooltip, String description) {
        this.value = value;
        this.foreground = foreground;
        this.aligh = aligh;
        this.tooltip = tooltip;
        this.description = description;
    }

    public Aligment getAligh() {
        return aligh;
    }

    public String getDescription() {
        return description;
    }

    public Color getForeground() {
        return foreground;
    }

    public String getTooltip() {
        return tooltip;
    }

    public String getValue() {
        return value;
    }

    public enum Aligment {
        Left(SwingUtilities.LEADING),
        Center(SwingUtilities.CENTER),
        Right(SwingUtilities.TRAILING);

        private int swingAligment;

        private Aligment(int swingAligment) {
            this.swingAligment = swingAligment;
        }

        public int getSwingAligment() {
            return swingAligment;
        }
    }

    public static StyledTableValue empty() {
        return EMPTY;
    }

    @Override
    public String toString() {
        if (value!=null){
            if (value.startsWith("<html>")){ //NOI18N
                StringBuilder result = new StringBuilder(value.length());
                int len = value.length();
                boolean skip = false;
                for(int i=0;i<len;i++){
                    char ch = value.charAt(i);
                    if (ch=='<'){
                        skip = true;
                        continue;
                    }
                    if (ch=='>'){
                        skip=false;
                        continue;
                    }
                    if (!skip){
                        result.append(ch);
                    }
                }
                return result.toString();
            } else {
                return value;
            }
        } else {
            return "";
        }
    }
}
